package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.MyMoney;
import com.sl.au.entity.MyMoneyType;
import com.sl.au.repository.MyMoneyRepository;
import com.sl.au.repository.MyMoneyTypeRepository;
import com.sl.au.service.MyMoneyService;


@Service("MyMoneyService")
@Transactional
public class MyMoneyServiceImpl extends BaseServiceImpl implements MyMoneyService {
	@Autowired
	MyMoneyRepository myMoneyRepository;
	@Autowired
	MyMoneyTypeRepository myMoneyTypeRepository;
	
	@Override
	public MyMoneyRepository getRepository() {
		// TODO Auto-generated method stub
		return this.myMoneyRepository;
	}
	@Override
	public List<MyMoney> getMoney(int start, int size,  Map<String, String> condition) {
		// TODO Auto-generated method stub
		Page<MyMoney> pages = null;
		String key = condition.get("key");
		String dataFilter = condition.get("dataFilter");
		String userId = condition.get("userId");
		String superior=condition.get("superior");
		String areaId=condition.get("areaId");
		Pageable pageable = new PageRequest(start / size, size, Sort.Direction.DESC, "createTime");
		if(dataFilter.equals("本部门")){
			pages = myMoneyRepository.getMoney(key,superior, pageable);
		}else if(dataFilter.equals("本片区")){
			pages = myMoneyRepository.getMoneySal(key,areaId, pageable);
		}else if(dataFilter.equals("所有")){
			pages = myMoneyRepository.getMoney(key, pageable);
		}
		return pages.getContent();
	}
	@Override
	public long getCount( Map<String, String> condition) {
		// TODO Auto-generated method stub
		String key = condition.get("key");
		String dataFilter = condition.get("dataFilter");
		String userId = condition.get("userId");
		String superior=condition.get("superior");
		String areaId=condition.get("areaId");
		long count = 0;
		if(dataFilter.equals("本部门")){
			count = myMoneyRepository.getCount(key,superior);
		}else if(dataFilter.equals("本片区")){
			count = myMoneyRepository.getCountSal(key,areaId);
		}else if(dataFilter.equals("所有")){
			count = myMoneyRepository.getCount(key);
		}
		return count;
	}
	@Override
	public void deleteAll(String id) {
		MyMoney myMoney = myMoneyRepository.findOne(id);
		Pageable pageable = new PageRequest(0 / 1000, 10000, Sort.Direction.DESC, "id");
		List<MyMoneyType> list = myMoneyTypeRepository.getMoneyType(id, pageable).getContent();
		for(MyMoneyType myMoneyType : list){
			myMoneyTypeRepository.delete(myMoneyType);
		}
		myMoneyRepository.delete(myMoney);
	}

}
